package cn.admobiletop.adsuyi.adapter.toutiao.data;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;

import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;

import cn.admobiletop.adsuyi.ad.data.ADSuyiNativeExpressAdInfo;
import cn.admobiletop.adsuyi.ad.error.ADSuyiError;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiNativeAdListener;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiNativeVideoListener;
import cn.admobiletop.adsuyi.ad.widget.ADSuyiInterceptContainer;
import cn.admobiletop.adsuyi.util.ADSuyiViewUtil;


/**
 * @author ciba
 * @description 信息流广告内容对象
 * @date 2020/4/3
 */
public abstract class NativeExpressAdInfo extends TTNativeExpressAdInfo<ADSuyiNativeAdListener>
        implements ADSuyiNativeExpressAdInfo,
        TTNativeExpressAd.ExpressAdInteractionListener,
        TTNativeExpressAd.ExpressVideoAdListener {
    private int width;
    private int height;
    private String posId;
    private ADSuyiNativeVideoListener adSuyiNativeVideoListener;
    private boolean isExposed;
    private ADSuyiInterceptContainer interceptContainer;
    private Handler handler = new Handler(Looper.getMainLooper());

    public NativeExpressAdInfo(String posId, int width, int height, Activity activity, String platformPosId) {
        super(activity, platformPosId);
        this.posId = posId;
        this.width = width;
        this.height = height;
    }

    @Override
    public void setAdapterAdInfo(TTNativeExpressAd ttNativeExpressAd) {
        super.setAdapterAdInfo(ttNativeExpressAd);

        initNativeExpressAd();
    }

    @Override
    public boolean isNativeExpress() {
        return true;
    }

    @Override
    public boolean isVideo() {
        return getAdapterAdInfo() != null && TTAdConstant.IMAGE_MODE_VIDEO == getAdapterAdInfo().getImageMode();
    }

    @Override
    public void setVideoListener(ADSuyiNativeVideoListener adSuyiNativeVideoListener) {
        if (isVideo()) {
            this.adSuyiNativeVideoListener = adSuyiNativeVideoListener;
            getAdapterAdInfo().setVideoAdListener(this);
        }
    }

    @Override
    public View getNativeExpressAdView(@NonNull ViewGroup container) {
        ADSuyiViewUtil.releaseClickTouchListener(container);
        if (interceptContainer == null) {
            if (getAdapterAdInfo() != null) {
                View expressAdView = getAdapterAdInfo().getExpressAdView();
                if (expressAdView != null) {
                    interceptContainer = new ADSuyiInterceptContainer(expressAdView.getContext());
                    interceptContainer.setLayoutParams(new ViewGroup.LayoutParams(width <= 0 ? ViewGroup.LayoutParams.MATCH_PARENT : width, height <= 0 ? ViewGroup.LayoutParams.WRAP_CONTENT : height));
                    interceptContainer.setPosId(posId);
                    interceptContainer.addResponseClickView(expressAdView);
                }
            }
        }
        return interceptContainer;
    }

    @Override
    public void render(@NonNull ViewGroup container) {
        getNativeExpressAdView(container);
    }

    @Override
    public void onVideoLoad() {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (adSuyiNativeVideoListener != null) {
                        adSuyiNativeVideoListener.onVideoLoad(NativeExpressAdInfo.this);
                    }
                }
            });
        }
    }

    @Override
    public void onVideoError(final int errorCode, final int extraCode) {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (adSuyiNativeVideoListener != null) {
                        adSuyiNativeVideoListener.onVideoError(NativeExpressAdInfo.this, new ADSuyiError(errorCode, "extraCode : " + extraCode));
                    }
                }
            });
        }
    }

    @Override
    public void onVideoAdStartPlay() {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (adSuyiNativeVideoListener != null) {
                        adSuyiNativeVideoListener.onVideoStart(NativeExpressAdInfo.this);
                    }
                }
            });
        }
    }

    @Override
    public void onVideoAdPaused() {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (adSuyiNativeVideoListener != null) {
                        adSuyiNativeVideoListener.onVideoPause(NativeExpressAdInfo.this);
                    }
                }
            });
        }
    }

    @Override
    public void onVideoAdContinuePlay() {

    }

    @Override
    public void onProgressUpdate(long l, long l1) {

    }

    @Override
    public void onVideoAdComplete() {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (adSuyiNativeVideoListener != null) {
                        adSuyiNativeVideoListener.onVideoComplete(NativeExpressAdInfo.this);
                    }
                }
            });
        }
    }

    @Override
    public void onClickRetry() {
        // 点击重试
    }

    @Override
    public void onCloseClick(View view) {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null) {
                        getAdListener().onAdClose(NativeExpressAdInfo.this);
                    }
                }
            });
        }

    }

    @Override
    public void releaseAdapter() {
        super.releaseAdapter();
        adSuyiNativeVideoListener = null;
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }

    @Override
    public void onAdClicked(View view, int i) {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null) {
                        getAdListener().onAdClick(NativeExpressAdInfo.this);
                    }
                }
            });
        }
    }

    @Override
    public void onAdShow(View view, int i) {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null && !isExposed) {
                        isExposed = true;
                        getAdListener().onAdExpose(NativeExpressAdInfo.this);
                    }
                }
            });
        }
    }

    /**
     * 设置广告事件监听
     */
    private void initNativeExpressAd() {
        if (getAdapterAdInfo() != null) {
            getAdapterAdInfo().setExpressInteractionListener(this);
            getAdapterAdInfo().render();
        }
    }

}
